package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.User;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public User getUserByIdByPass(String userId, String password) throws SQLException {
        User user =  null;
        String sql = "SELECT*FROM `user` WHERE userId = ? AND `password` = ?";
        DBUtil.getConnection();
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将？替换为具体的值
            pst.setString(1,userId);
            pst.setString(2,password);
            //执行查询并将查询到的结果放到结果集
            rs = pst.executeQuery();
            //遍历结果集并将结果集中的每行数据保存到List集合中
            while (rs.next()){
                user = new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("userImg"));
                user.setDelTag(rs.getInt("delTag"));
            }

        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return user;
    }

    @Override
    public int getUserById(String userId) throws SQLException {
        int i = 0;
        String sql = "SELECT COUNT(*) AS nums FROM `user` WHERE userId = ?";
        DBUtil.getConnection();
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将？替换为具体的值
            pst.setString(1,userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                i = rs.getInt("nums");
            }
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }

        return i;
    }

    @Override
    public int saveUser(String userId, String password, String userName, Integer userSex) throws SQLException {
        int i = 0;
        String sql = "INSERT INTO `user`(userId,`password`,userName,userSex) VALUES(?,?,?,?)";
        DBUtil.getConnection();
        try {
            //Connection从ThreadLocal中获取
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            //将？替换为具体的值
            pst.setString(1,userId);
            pst.setString(2,password);
            pst.setString(3,userName);
            pst.setInt(4,userSex);
            //执行查询并将查询到的结果放到结果集
            i = pst.executeUpdate();
        }finally {                          //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(pst);              //这里负责关闭PreparedStatement和ResultSet
        }
        return i;
    }
}
